﻿using System.Xml.Linq;

namespace Phoenix.Xna.Framework.Serialization.Markup.Objects
{
    /// <summary>
    /// The intermediary markup object for value types.
    /// </summary>
    internal class ValueMarkup : MarkupObjectBase
    {
        #region IMarkupElement Members

        /// <summary>
        /// Serialize the element to XML.
        /// </summary>
        /// <returns>The element markup serialzed as an XElement.</returns>
        public override XElement Serialize()
        {
            XElement element = new XElement(NamespaceCache.GetNamespaceFor(ObjectType) + ObjectType.Name);
            element.Value = Instance.TypeConvertionCast<string>();
            return element;
        }

        /// <summary>
        /// Deserialize the element from an XElement.
        /// </summary>
        /// <param name="xml">The XElement to deserialize this element from.</param>
        public override void Deserialize(XElement xml)
        {
            XElement xmlInstance = xml;
            Instance = xmlInstance.Value.TypeConvertionCast(ObjectType);
        }

        #endregion

        #region IMarkupObject Members

        /// <summary>
        /// Convert this MarkupObject in to an object model and sets the Instance.
        /// </summary>
        public override void ConstructInstance()
        {
            //Do nothing; should have been done during deserialization.
        }

        /// <summary>
        /// Extract the markup information from the specified object.
        /// </summary>
        /// <param name="instance">The instance to extract markup information for.</param>
        public override void ExtractMarkup(object instance)
        {
            Instance = instance;
            ObjectType = instance.GetType();
        }

        #endregion
    }
}
